package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("/list")
    @ApiOperation("分页商品")
    public PageDTO pageByItem(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        log.info("商品的分页查询");
        Page<Item> result = itemService.page(new Page<>(page, size));
        return new PageDTO(result.getTotal(),result.getRecords());

    }
    @GetMapping("/id")
    @ApiOperation("根据id查询商品的信息")
    public Item selectById(@RequestParam("id") long id){
        log.info("根据商品id查询商品：{}",id);
        return  itemService.getById(id);
    }
    @PostMapping
    @ApiOperation("新增商品")
    public Item addItem(@RequestBody Item item){
        log.info("新增商品：{}",item);
        item.setStatus(2);
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);
        return null;
    }

    @PutMapping("/status/{id}/{status}")
    @ApiOperation("根据id修改商品的状态")
    public Item updateStatus(@RequestParam("id") long id,@RequestParam("status") Integer status){
        log.info("根据id修改商品的状态:{}",id);
         itemService.updateStatus(id,status);
         return null;
    }
    @PutMapping()
    @ApiOperation("修改商品的信息")
    public Item updateItem(@RequestBody Item item){
        log.info("修改商品信息");
        item.setUpdateTime(new Date());
        item.setStatus(null);
        itemService.updateById(item);
        return null;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除商品信息")
  public Item deleteItemById(@RequestParam("id") long id){
        log.info("根据id删除商品：{}",id);
        itemService.deleteById(id);
        return null;
    }
    }




